Buzzer application to show basic interaction


This simple buzzer application creates a large button on the screen and creates the basic interactivity between the Wiimote and the application.

Here is the completed application buzzerMote_app.fla with commented code, and below is just the code with comments. (Not sure why the Word document icon is there it is a .fla file...)

//import the required classes this is the same for all applications that use the WiiFlash Server
//no changes should be, or need to be, made here.
import org.wiiflash.Wiimote;
import org.wiiflash.events.ButtonEvent;
import org.wiiflash.events.WiimoteEvent;
import flash.events.*;
 
 
//This sets up the new Wiimote connection
var myWiimote:Wiimote = new Wiimote();
myWiimote.connect ();
 
//When Flash detects the successful connection call the onWiinoteConnect function
myWiimote.addEventListener( Event.CONNECT, onWiimoteConnect );
 
 
function onWiimoteConnect ( pEvent:Event ):void
{
    //I have made a small change to the original code I have decided to attach all the events when the Wiimote
    //has connected. I have found this useful when using more than 1 Wiimote and it works just fine with 1 as well.
    //pEvent.target is the Wiimote itself
    //therefore when the A button is pressed it will call the onAPressed function etc.
    pEvent.target.addEventListener( ButtonEvent.A_PRESS, onAPressed );
    pEvent.target.addEventListener( ButtonEvent.A_RELEASE, onAReleased);
}
 
//I have removed the connection error and connection closed functions and also the add synchronise movieClip.
//The synchronise clip basically tells you if the Wiimotes are connected and when the connection is broken.
//The connection error and closed functions are useful if you want something to happen in your applicaton
//if either of these thigs are true. I felt that they weren't needed in my examples as I wanted to demonstrate
//only what is important to the function of the current application.
//perhaps include them in yours, it may make your application useable if there is a problem connecting, perhaps
//it enables some mouse functions....
 
function onAPressed ( pEvt:ButtonEvent ):void
{
    //change the frame number and therefore graphical look of the buzzer movie clip, there is also a sound on that frame.
    buzzer1_mc.gotoAndStop(2);
    //vibrate the remote when pressed for 500 milliseconds or 1/2 a second
    myWiimote.rumbleTimeout = 500;
}
 
function onAReleased ( pEvt:ButtonEvent ):void
{
    //change the frame number and therefore graphical look of the buzzer movie clip
    buzzer1_mc.gotoAndStop(1);
}